package yxy.game.pm2.command.threelinemutualpush;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ThreeLineMutualPushMessage;
import yxy.game.pm2.module.threelinepush.ThreeLinePushModule;

/****************************************
 * @name CMD_ThreeLineMutualPushMapIdRecord
 * @description 三线互推小游戏关卡记录
 * @author 梁健彬
 * @since 2022-08-17
 ***************************************/
@MessageType(proto = Command.ThreeLineMutualPushMapIdRecord, classtype = ThreeLineMutualPushMessage.ThreeLineMutualPushMapIdRecordRequest.class)
public class CMD_ThreeLineMutualPushMapIdRecord extends Command<ThreeLineMutualPushMessage.ThreeLineMutualPushMapIdRecordRequest> {

    @Override
    public void execute(MessageSource messageSource, ThreeLineMutualPushMessage.ThreeLineMutualPushMapIdRecordRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        int mapId = request.getMapId();
        ThreeLineMutualPushMessage.ThreeLineMutualPushMapIdRecordResponse.Builder response = ThreeLineMutualPushMessage.ThreeLineMutualPushMapIdRecordResponse.newBuilder();

        try {

            ThreeLinePushModule module = ThreeLinePushModule.get(playerCode);

            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            module.setCurrentMapId(mapId);
            module.set();
            response.setCurrentMapId(module.getCurrentMapId());
            response.addAllHasReceiveIds(module.getRewardIds());

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);

        }
    }

    private void send(MessageSource messageSource, ThreeLineMutualPushMessage.ThreeLineMutualPushMapIdRecordResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


}
